import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-08-15
 * Time:22:48
 */
public class HomeWorkDelete {
        public static void main(String[] args) throws SQLException {
            DataSource dataSource = new MysqlDataSource();
            ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
            ((MysqlDataSource)dataSource).setUser("root");
            ((MysqlDataSource)dataSource).setPassword("wgz20030828");

            //连接
            Connection connection = dataSource.getConnection();
            System.out.println(connection);


            // ? 作为占位符. 占个位置, 后面会替换成其他的值.
            // 删除id最大的一条借阅记录
            String sql = "delete from borrow_info where id = (select r." +
                    "id from (select max(id) id from borrow_info) r)";
            //预处理
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            //执行
            int n = preparedStatement.executeUpdate();
            //查看执行的行数
            System.out.println("n = "+ n);

            //完成之后, 就需要关闭释放相关资源.
            preparedStatement.close();
            connection.close();

        }
    }

